package org.qydjk.common.service.impl;


import org.qydjk.common.dao.IBaseDao;
import org.qydjk.common.service.IBaseService;
import org.qydjk.common.util.ListUtil;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class BaseService implements IBaseService {
    @Autowired
    protected IBaseDao baseDao;

    public IBaseDao getBaseDao() {
        return baseDao;
    }
    public void setBaseDao(IBaseDao baseDao) {
        this.baseDao = baseDao;
    }

    public <T> Serializable save(T t) throws Exception {
        return baseDao.save(t);
    }

    public <T> void update(T t) throws Exception {
        baseDao.update(t);
    }

    public <T> void delete(T t) throws Exception {
        baseDao.delete(t);
    }

    public <T> void deleteAll(Collection<T> ts) throws Exception {
        baseDao.deleteAll(ts);
    }

    public <T> void saveOrUpdate(T t) throws Exception {
        baseDao.saveOrUpdate(t);
    }

    public <T> T load(Class<T> t, Serializable id) throws Exception {
        return baseDao.load(t,id);
    }

    public <T> T get(Class<T> t, Serializable id) throws Exception {
        return baseDao.get(t,id);
    }

    public <T> List<T> findByHql(String hql, Object... values) throws Exception {
        return baseDao.findByHql(hql,values);
    }

    public <T> T findSingleByHql(String hql, Object... values) throws Exception{
        List<T> list = baseDao.findByHql(hql, values);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    public <T> List<T> findByHql(String hql, HibernatePage pager, Object... values) throws Exception{
        return baseDao.findByHql(hql,pager,values);
    }

    public <T> List<T> findByExample(T t) throws Exception{
        return baseDao.findByExample(t);
    }

    public <T> List<T> findBySQL(String sql, Class<?> cls, Object... values) throws Exception{
        return baseDao.findBySQL(sql,cls,values);
    }

    public <T> T findSingleBySQL(String sql, Class<?> cls, Object... values) throws Exception{
        return baseDao.findSingleBySQL(sql,cls,values);
    }

    public <T> List<T> findBySQL(String sql, Class<?> cls, HibernatePage pager, Object... values) throws Exception{
       return baseDao.findBySQL(sql,cls,pager,values);
    }

    public <T> List<T> findBeanBySQL(String sql, Class<?> cls, Object... values) throws Exception{
        return baseDao.findBeanBySQL(sql,cls,values);
    }

    public <T> T findSingleBeanBySQL(String sql, Class<?> cls, Object... values) throws Exception{
        return baseDao.findSingleBeanBySQL(sql,cls,values);
    }

    public <T> List<T> findBeanBySQL(String sql, Class<?> cls, HibernatePage pager, Object... values) throws Exception{
        return baseDao.findBeanBySQL(sql,cls,pager,values);
    }

    public List<Map<String,Object>> findMapBySQL(String sql, Object... values) throws Exception{
        return baseDao.findMapBySQL(sql,values);
    }

    public List<Map<String,Object>> findMapBySQL(String sql, HibernatePage pager, Object... values) throws Exception{
        return baseDao.findMapBySQL(sql,pager,values);
    }

    public int excuteSQL(String sql) throws Exception{
        return baseDao.excuteSQL(sql);
    }

    public int excuteHQL(String hql) throws Exception{
        return baseDao.excuteHQL(hql);
    }

    public <T> int getRecordCountHQL(String hql,Object... values) throws Exception{
        return baseDao.getRecordCountHQL(hql,values);
    }

    public <T> int getRecordCountSQL(String sql,Object... values) throws Exception{
        return baseDao.getRecordCountSQL(sql,values);
    }

    public <T> T getUniqueResultHQL(String hql,Object... values) throws Exception{
        return baseDao.getUniqueResultHQL(hql,values);
    }

    public <T> T getUniqueResultSQL(String sql,Object... values) throws Exception{
        return baseDao.getUniqueResultSQL(sql,values);
    }

    public List<Object> findSingleColumnListBySQL(String sql, Object... values) throws Exception{
        List<List<Object>> list = baseDao.findSingleColumnListBySQL(sql, values);
        return ListUtil.wipeInnerList(list);
    }

    public List<Object> findSingleColumnListBySQL(String sql, HibernatePage pager, Object... values) throws Exception{
        List<List<Object>> list = baseDao.findSingleColumnListBySQL(sql,pager,values);
        return ListUtil.wipeInnerList(list);
    }
}
